import { Toast } from 'vant'

export default function speech(options = { engine: 'baidu' }) {

  options = Object.assign({ engine: 'baidu' }, options)

  return new Promise((resolve, reject) => {
    if (typeof window.plus !== "undefined") {

      window.plus.speech.startRecognize(options, text => {
        resolve(text)
      }, e => {
        Toast({ message: `语音识别失败：${e.message}`, position: 'bottom' })
        reject(e)
      })

    } else {
      Toast({ message: '调用失败，请重试', position: 'bottom' })
      reject('plus未加载')
    }
  })

}